package com.train.trainmanage.conf;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.train.trainmanage.model.UserModel;

public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
		
		if(request.getRequestURI().equals("/user/login") ||request.getRequestURI().equals("/user/loginIn")
				||request.getRequestURI().equals("/user/vueList")||request.getRequestURI().equals("/user/vueUserInfo")) {
			return true;
		}
		HttpSession session = request.getSession();
		UserModel user = (UserModel) session.getAttribute("user");
		if(user == null ) {
			System.out.println("**********  进入登录拦截器 ，未登录，跳登录页面      ***************");
			response.sendRedirect(request.getContextPath() + "/user/login");
			return false;
		}
		
		return true;
	}

}
